package com.cdy.server.handler;

import com.cdy.common.command.Command;
import com.cdy.common.command.DownloadCommand;
import com.cdy.common.domain.FileEntity;
import com.cdy.common.domain.FileSectionEntity;
import com.cdy.common.response.DownloadResponse;
import com.cdy.store.manager.FileSystem;
import com.cdy.store.repository.FileRepository;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

import static com.cdy.common.property.CommonProperty.SmallSectionLength;

/**
 * 下载命令的处理器
 *
 * Created by 陈东一
 * 2019-10-25 13:37:57
 */
@Slf4j
public class DownloadCommandHandler implements CommandHandler {
    private final FileRepository fileRepository;
    private final FileSystem fileSystem;

    public DownloadCommandHandler(FileRepository fileRepository, FileSystem fileSystem) {
        this.fileRepository = fileRepository;
        this.fileSystem = fileSystem;
    }
    
    @Override
    public void handle(ChannelHandlerContext ctx, Command msg) {
        DownloadCommand downloadCommand = (DownloadCommand) msg;

        Long sectionId = downloadCommand.getSectionId();
        FileSectionEntity fileSectionEntity = fileRepository.getTaskById(sectionId);
        FileEntity fileEntity = fileRepository.getFileById(fileSectionEntity.getFileId());
        try {
            File file = fileSystem.findFile(fileEntity.getName() +"-"+ fileSectionEntity.getId());

            for (int i = downloadCommand.getBitBegin(); i < downloadCommand.getBitSize(); i++) {
                DownloadResponse downloadResponse = new DownloadResponse();
                downloadResponse.setBegin(downloadCommand.getBegin() + i * SmallSectionLength);
                downloadResponse.setSectionId(downloadCommand.getSectionId());
                downloadResponse.setFileMD5(fileEntity.getMd5());
                downloadResponse.setSectionBegin(downloadCommand.getBegin());
                downloadResponse.setBitBegin(downloadCommand.getBitBegin());
                downloadResponse.setBitSize(downloadCommand.getBitSize());
                downloadResponse.setBitTotal(downloadCommand.getBitTotal());
                int length = Math.toIntExact(SmallSectionLength);
                if (downloadCommand.getBitTotal() == 1 || i == downloadCommand.getBitTotal() - 1) { //如果是最后一位,那么长度用 总长度-前面的长度即可
                    length = Math.toIntExact(downloadCommand.getSectionLength() - i * SmallSectionLength);
                }
                ByteBuf byteBuf = fileSystem.readFile(file, downloadCommand.getBegin(), length);
                downloadResponse.setLength((long) byteBuf.capacity());
                downloadResponse.setByteBuf(byteBuf);
                if (i == downloadCommand.getBitSize()-1) {
                    downloadResponse.setLast(true);
                }
                write(ctx, downloadResponse);
            }

        } catch (Exception e) {
            // todo
            // 断开连接? 响应失败?
        }

    }



}
